<template>
	<view class="my-store-info">
		<view class="store-box">
			<view class="title">*身份证件信息</view>
			<view class="store-form">
				<u-cell-group>
					<u-cell title="人面照">
						<u-upload :fileList="facadeList" @afterRead="afterReadfacade" @delete="deletePicfacade" name="1"
							slot="value" :multiple="false" :maxCount="1" width="200" height="120">
							<image :src="shopMallImgUrl+'cardA.png'" mode="widthFix" style="width: 200px;height: 120px;"></image>
						</u-upload>

					</u-cell>
					<u-cell title="国徽照">
						<u-upload :fileList="receptionList" @afterRead="afterReadrecep" @delete="deletePicrecep"
							name="2" slot="value" :multiple="false" :maxCount="1" width="200" height="120">
							<image :src="shopMallImgUrl+'cardB.png'" mode="widthFix" style="width: 200px;height: 120px;"></image>
						</u-upload>
					</u-cell>

				</u-cell-group>
			</view>
		</view>
		<view class="store-box" v-if="parameter.merchantBusinessType==3">
			<view class="title">*手持身份证照</view>
			<view class="store-form">
				<u-cell-group>
					<u-cell title="手持照">
						<u-upload :fileList="imgIdcardHoldList" @afterRead="afterReadIdcardHold"
							@delete="deletePicIdcardHold" name="1" slot="value" :multiple="false" :maxCount="1"
							width="200" height="120">
							<image :src="shopMallImgUrl+'cardA.png'" mode="widthFix" style="width: 200px;height: 120px;"></image>
						</u-upload>

					</u-cell>


				</u-cell-group>
			</view>
		</view>
		<view class="store-box">
			<!-- <view class="title">*联系方式</view> -->
			<view class="store-form">
				<u-cell-group>
					<u-cell title="法人姓名">
						<u--input slot="value" placeholder="法人姓名" fontSize="14px" border="none"
							v-model="storeInfo.artifNm"></u--input>
					</u-cell>
					<u-cell title="身份证号">
						<u--input slot="value" placeholder="身份证号" fontSize="14px" border="none"
							v-model="storeInfo.legalidnum"></u--input>
					</u-cell>
					<u-cell title="证件开始时间">
						<u--input slot="value" placeholder="(格式:2000-01-01)" fontSize="14px" border="none"
							v-model="storeInfo.legalidnumStart"></u--input>
						<!-- <picker mode="date" slot="value" :value="storeInfo.legalidnumStart" :start="startDate"
							:end="endDate" @change="bindDateChangeStart">
							<view class="uni-input">0{{storeInfo.legalidnumStart}}</view>
						</picker> -->
					</u-cell>
					<u-cell title="证件有效期">
						<u--input slot="value" placeholder="(格式:2000-01-01,不填为长期)" fontSize="14px" border="none"
							v-model="storeInfo.legalidnumexpire"></u--input>
						<!-- <picker mode="date" slot="value" :value="storeInfo.legalidnumexpire" :start="startDate"
							:end="endDate" @change="bindDateChangeEnd">
							<view class="uni-input">0{{storeInfo.legalidnumexpire}}</view>
						</picker> -->
					</u-cell>
					<u-cell title="法人证件地址">
						<u--input slot="value" placeholder="法人证件地址" fontSize="14px" border="none"
							v-model="storeInfo.legalAddr"></u--input>
					</u-cell>
				</u-cell-group>
			</view>
		</view>
		<view class="btn" @tap="jump()">下一步</view>
	</view>
</template>

<script>
	const app = getApp();
	const globalData = app.globalData;
	const apiUrl = globalData.wxApi;
	const wxImg = globalData.wxImg;
	import {
		getStoreData,
		dealNum,
		httpPost,
		httpGet,
		bdMapToTxMap,
		txMapToBdMap,
	} from "@/utils/http.js";
	import mpMixin from "@/uni_modules/uview-ui/libs/mixin/mpMixin";
	const pic1 = require(`@/img/my/my1.png`);
	import popupActivity from "./component/popupActivity";
import {shopMallImgUrl} from "@/common/config.js"

	export default {
		data() {
			return {
				shopMallImgUrl:shopMallImgUrl,
				wxImg,
				apiUrl,
				dealNum,
				pic1,
				cityFlag: false,
				value: "",
				mode: "single",
				storeInfo: {
					legalidnum: "", //法人身份证号
					imgIdcardA: "", //法人身份证正面
					imgIdcardB: "", //法人身份证反面照片
					legalidnumStart: "", //法人身份证开始日期
					legalidnumexpire: "", //法人身份证有效期
					artifNm: "", //法人名称
					imgIdcardHolding: "", //手持身份证照
					merchantIdNo: "", //结算人身份证号
					merchantIdExpire: "", //结算人身份证有效期
					imgPrivateIdcardA: "", //对私账户身份证正面照片
					imgPrivateIdcardB: "" ,//对私账户身份证反面照片
					legalAddr:""//法人证件地址
				},
				parameter: {},
				userInfo: {},
				flag: false,
				title: "",
				showType: false,
				typeText: "",
				mainBusiness: "",
				weiXinBusiness: "",
				facadeList: [],
				receptionList: [],
				showMainCategory: false,
				MainCategoryList: [],
				showWeiXinCategory: false,
				WeiXinCategoryList: [],
				WeiXinCategoryListTwo: [],
				isShowTwo: false,
				isShowThree: false,
				facadeImgs: [],
				receptionImgs: [],
				licenseList: [],
				licenseImg: [],
				imgIdcardHoldList: [],
				IdcardHoldImgs: []

			};
		},
		components: {
			popupActivity,
		},
		computed: {
			// startDate() {
			// 	return this.getDate('start');
			// },
			// endDate() {
			// 	return this.getDate('end');
			// }
		},
		onShow() {

		},
		onUnload() {

		},
		onLoad(options) {
			console.log(options)

			if (options && options.parameter) {
				this.parameter = JSON.parse(options.parameter);
				console.log(99, this.parameter)
			}
			this.getStoreinfo();

		},
		methods: {
			getStoreinfo() {
				let that = this;
				httpGet("/api/sellerstore/selfInfo")
					.then((res) => {
						if (res.data) {
							that.storeInfo.legalidnum = res.data.legalidnum; //法人身份证号
							that.storeInfo.imgIdcardA = res.data.imgIdcardA; //法人身份证正面
							that.storeInfo.imgIdcardB = res.data.imgIdcardB; //法人身份证反面照片
							that.storeInfo.legalidnumStart = res.data.legalidnumStart; //法人身份证开始日期
							that.storeInfo.legalidnumexpire = res.data.legalidnumexpire; //法人身份证有效期
							that.storeInfo.artifNm = res.data.artifNm; //法人名称
							that.storeInfo.imgIdcardHolding = res.data.imgIdcardHolding; //手持身份证照
                             that.storeInfo.legalAddr=res.data.legalAddr;//法人证件地址
							that.storeInfo.imgPrivateIdcardA = res.data.imgPrivateIdcardA; //对私账户身份证正面照片
							that.storeInfo.imgPrivateIdcardB = res.data.imgPrivateIdcardB; //对私账户身份证反面照片
							that.storeInfo.merchantIdNo = res.data.merchantIdNo; //结算人身份证号码
							that.storeInfo.merchantIdExpire = res.data.merchantIdExpire; //结算人身份证有效期
							if (that.storeInfo.imgIdcardA) {
								let imgIdcardAArr = that.storeInfo.imgIdcardA.split(','); //身份证正面
								if (imgIdcardAArr.length != 0) {
									that.facadeList = imgIdcardAArr.map((item) => {
										let obj = {};
										obj.url = wxImg + item;
										return obj
									});
								}
							}
							if (that.storeInfo.imgIdcardB) {
								let imgIdcardBArr = that.storeInfo.imgIdcardB.split(','); //身份证反面
								if (imgIdcardBArr.length != 0) {
									that.receptionList = imgIdcardBArr.map((item) => {
										let obj = {};
										obj.url = wxImg + item;
										return obj
									});
								}
							}

							if (that.storeInfo.imgIdcardHolding) {
								let imgIdcardHoldingArr = that.storeInfo.imgIdcardHolding.split(','); //手持身份证
								if (imgIdcardHoldingArr.length != 0) {
									that.imgIdcardHoldList = imgIdcardHoldingArr.map((item) => {
										let obj = {};
										obj.url = wxImg + item;
										return obj
									});
								}
							}





						}
					})
					.catch((e) => {});
			},
			async afterReadfacade(event) {
				//console.log(event)
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [];
				lists[0] = event.file;
				let fileListLen = this.facadeList.length;
				lists.map((item) => {
					this.facadeList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					//console.log('666',result)
					//this.facadeImgs.push(result[0].path+'/'+name);
					let item = this.facadeList[fileListLen]
					this.facadeList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				console.log(555, this.facadeList);
				this.ocrIdentify(this.facadeList[0].thumb)
				this.getFacadeImgList(this.facadeList);

			},
			ocrIdentify(tempFilePath) {
				let that=this;
				const invokeRes = wx.serviceMarket.invokeService({
					service: 'wx79ac3de8be320b71',  //固定的不要动
					api: 'OcrAllInOne',             //固定的不要动
					data: {
						// 用 CDN 方法标记要上传并转换成 HTTP URL 的文件
						img_url: new wx.serviceMarket.CDN({
							type: 'filePath',
							filePath: tempFilePath,
						}),
						data_type: 3,        
						ocr_type: 1        // 1是身份证识别  7是营业执照识别
					},
					success: function(res) {   //打印这个res  就是你想要的结果
					console.log(666,res)
						if(res.errMsg=="invokeService:ok"){
							that.storeInfo.legalidnum=res.data.idcard_res.id.text;//身份证号
							that.storeInfo.artifNm=res.data.idcard_res.name.text;//姓名
							that.storeInfo.merchantIdNo=res.data.idcard_res.id.text;//身份证号
						}
					}

				})
                

			},
			deletePicfacade(event) {
				this.facadeList.splice(event.index, 1)
				this.getFacadeImgList(this.facadeList);
			},
			getFacadeImgList(list) {
				console.log(list)
				if (list.length == 0) {
					this.facadeImgs = [];
				} else {
					this.facadeImgs = list.map((item) => {
						let imgUrl = item.url[0].path + '/' + item.url[0].name;
						return imgUrl
					})
				}
				this.storeInfo.imgIdcardA = this.facadeImgs.join(",");
				console.log(this.facadeImgs)
			},
			async afterReadrecep(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [];
				lists[0] = event.file;
				let fileListLen = this.receptionList.length
				lists.map((item) => {
					this.receptionList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.receptionList[fileListLen]
					this.receptionList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				//this.ocrIdentify(this.receptionList[0].thumb)
				this.getReceptImgList(this.receptionList);
			},
			deletePicrecep(event) {
				this.receptionList.splice(event.index, 1);
				this.getReceptImgList(this.receptionList);
			},
			getReceptImgList(list) {
				// console.log(list)
				if (list.length == 0) {
					this.receptionImgs = [];
				} else {
					this.receptionImgs = list.map((item) => {
						let imgUrl = item.url[0].path + '/' + item.url[0].name;
						return imgUrl
					})
				}
				this.storeInfo.imgIdcardB = this.receptionImgs.join(",");
			},
			async afterReadIdcardHold(event) {
				//console.log(event)
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [];
				lists[0] = event.file;
				let fileListLen = this.imgIdcardHoldList.length;
				lists.map((item) => {
					this.imgIdcardHoldList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)

					let item = this.imgIdcardHoldList[fileListLen]
					this.imgIdcardHoldList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}

				this.getIdcardHoldList(this.imgIdcardHoldList);

			},
			deletePicIdcardHold(event) {
				this.imgIdcardHoldList.splice(event.index, 1)
				this.getIdcardHoldList(this.imgIdcardHoldList);
			},
			getIdcardHoldList(list) {
				// console.log(list)
				if (list.length == 0) {
					this.IdcardHoldImgs = [];
				} else {
					this.IdcardHoldImgs = list.map((item) => {
						let imgUrl = item.url[0].path + '/' + item.url[0].name;
						return imgUrl
					})
				}
				this.storeInfo.imgIdcardHolding = this.IdcardHoldImgs.join(",");
				// console.log(this.IdcardHoldImgs)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://api.chain-pay.cn/api/upload/store',
						filePath: url,
						name: 'file',
						header: {
							"Authorization": uni.getStorageSync("token")
						},

						success: (res) => {
							let imgObj = JSON.parse(res.data);
							// console.log(JSON.parse(res.data))
							setTimeout(() => {
								resolve(imgObj.data)
							}, 1000)
						}
					});
				})
			},


			jump() {
				if (!this.storeInfo.imgIdcardA) {
					wx.showToast({
						icon: "none",
						title: "请上传法人身份证正面"
					});
					return;
				} else if (!this.storeInfo.imgIdcardB) {
					wx.showToast({
						icon: "none",
						title: "请上传法人身份证反面"
					});
					return;
				} else if (!this.storeInfo.artifNm) {
					wx.showToast({
						icon: "none",
						title: "请输入法人姓名"
					});
					return;
				} else if (!this.storeInfo.legalidnum) {
					wx.showToast({
						icon: "none",
						title: "请输入法人身份证号"
					});
					return;
				} else if (!this.storeInfo.legalidnumStart) {
					wx.showToast({
						icon: "none",
						title: "请选择法人身份证开始时间"
					});
					return;
				} else if (!this.storeInfo.legalidnumexpire) {
					wx.showToast({
						icon: "none",
						title: "请选择法人身份证有效期"
					});
					return;
				}
				if (this.parameter.merchantBusinessType == 3) {
					if (!this.storeInfo.imgIdcardHolding) {
						wx.showToast({
							icon: "none",
							title: "请上传手持身份证照"
						});
						return;
					}
				}
				this.storeInfo.imgPrivateIdcardA = this.storeInfo.imgIdcardA; //对私账户身份证正面照片
				this.storeInfo.imgPrivateIdcardB = this.storeInfo.imgIdcardB; //对私账户身份证反面照片
				this.storeInfo.merchantIdNo = this.storeInfo.legalidnum; //结算人身份证号码
				this.storeInfo.merchantIdExpire = this.storeInfo.legalidnumexpire; //结算人身份证有效期
				let params = {
					...this.parameter,
					...this.storeInfo
				};
				uni.setStorageSync('corporateInfo', this.storeInfo);
				wx.navigateTo({
					url: "/pages/storeManage/settlementInfo?parameter=" + JSON.stringify(params),
				});
			

			},
			


		},
	};
</script>

<style lang="scss">
	.my-store-info {
		background-color: #f4f5f7;
		padding-top: 20rpx;
	}

	.store-box {
		width: 650rpx;
		margin-left: 30rpx;
		background-color: #fff;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		padding: 20rpx;

		.title {
			font-size: 28rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}
	}

	/deep/ .u-cell__title-text {
		font-size: 28rpx;
	}

	/deep/ .u-input {
		flex: 2
	}

	.tylist {
		padding: 30rpx;

		.li {
			padding-top: 20rpx;
			padding-bottom: 20rpx;
			font-size: 28rpx;
			border-bottom: 2rpx solid #f4f5f7;
		}

		.li-one-box {

			.li-one-title {
				padding-top: 20rpx;
				padding-bottom: 20rpx;
				font-size: 28rpx;
				border-bottom: 2rpx solid #f4f5f7;
			}

			.li-two-box {
				margin-left: 20rpx;

				.li-two-title {
					padding-top: 20rpx;
					padding-bottom: 20rpx;
					font-size: 28rpx;
					border-bottom: 2rpx solid #f4f5f7;
				}

				.li-three-box {
					margin-left: 30rpx;

					.li-three-title {
						padding-top: 20rpx;
						padding-bottom: 20rpx;
						font-size: 28rpx;
						border-bottom: 2rpx solid #f4f5f7;
					}
				}
			}
		}
	}

	.btn {
		width: 690rpx;
		height: 100rpx;
		line-height: 100rpx;
		font-weight: bold;
		color: #fff;
		text-align: center;
		background: linear-gradient(to right, #3CBAF6, #56D1FC);
		margin-left: 30rpx;
	}
</style>